package com.example.yichuangyun.mvvm.ui.activity;

import android.annotation.SuppressLint;
import android.os.Bundle;

import androidx.recyclerview.widget.LinearLayoutManager;

import com.example.yichuangyun.R;
import com.example.yichuangyun.config.Constant;
import com.example.yichuangyun.databinding.ActivityRiskInfoBinding;
import com.example.yichuangyun.model.company.CompanyLabelModel;
import com.example.yichuangyun.model.company.RiskInfoDataModel;
import com.example.yichuangyun.mvvm.base.BaseActivity;
import com.example.yichuangyun.mvvm.ui.activity.view.RiskInfoViewModel;
import com.example.yichuangyun.mvvm.ui.adapter.InfoTabAdapter;
import com.example.yichuangyun.mvvm.ui.adapter.RiskInfoAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * 风险信息
 */
@SuppressLint("NotifyDataSetChanged")
public class RiskInfoActivity extends BaseActivity<ActivityRiskInfoBinding, RiskInfoViewModel> {

    private final List<CompanyLabelModel> tabData = new ArrayList<>();
    private InfoTabAdapter tabAdapter;
    private final List<RiskInfoDataModel> data = new ArrayList<>();
    private int mPos;
    private LinearLayoutManager manager;

    @Override
    protected RiskInfoViewModel bindModel() {
        return getViewModel(this, RiskInfoViewModel.class);
    }

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    @Override
    protected void initData() {
        initTitleBar(getString(R.string.fxxx));
        tabData.add(new CompanyLabelModel(getString(R.string.jyyc)));
        tabData.add(new CompanyLabelModel(getString(R.string.xzcf)));
        tabData.add(new CompanyLabelModel(getString(R.string.yzwfsx)));
        tabData.add(new CompanyLabelModel(getString(R.string.swyc)));

        data.add(new RiskInfoDataModel(getString(R.string.jyyc), 0, false));
        data.add(new RiskInfoDataModel(getString(R.string.xzcf), 0, false));
        data.add(new RiskInfoDataModel(getString(R.string.yzwfsx), 0, false));
        data.add(new RiskInfoDataModel(getString(R.string.swyc), 0, false));

        manager = new LinearLayoutManager(this);
        binding.jyycRecyclerView.setLayoutManager(manager);

        Bundle bundle = getIntent().getExtras();
        if (null != bundle) {
            String eid = bundle.getString(Constant.eidKey);
            int position = bundle.getInt(Constant.positionKey);
            tabData.get(position).setSelect(true);
            data.get(position).setOpen(true);
            LinearLayoutManager linearLayoutManager = (LinearLayoutManager) binding.recyclerView.getLayoutManager();
            if (null != linearLayoutManager) {
                linearLayoutManager.scrollToPositionWithOffset(position, 0);
            }
            viewModel.getRiskInfo(eid);
        }
    }

    @Override
    protected void initClick() {
        viewModel.riskInfoDataModelMutableLiveData.observe(this, riskInfoDataModel -> {
            if (null != riskInfoDataModel.getTabnormalList() && riskInfoDataModel.getTabnormalList().size() > 0) {
                data.get(0).setTabnormalList(riskInfoDataModel.getTabnormalList());
                tabData.get(0).setCount(riskInfoDataModel.getTabnormalList().size());
            }
            if (null != riskInfoDataModel.getTadministrativePunishments() && riskInfoDataModel.getTadministrativePunishments().size() > 0) {
                data.get(1).setTadministrativePunishments(riskInfoDataModel.getTadministrativePunishments());
                tabData.get(1).setCount(riskInfoDataModel.getTadministrativePunishments().size());
            }
            if (null != riskInfoDataModel.getTseriousIllegals() && riskInfoDataModel.getTseriousIllegals().size() > 0) {
                data.get(2).setTseriousIllegals(riskInfoDataModel.getTseriousIllegals());
                tabData.get(2).setCount(riskInfoDataModel.getTseriousIllegals().size());
            }

            // 税务异常-非正常户表 + 税务异常-重大税收违法
            int count = 0;
            List<RiskInfoDataModel.RiskModel> totalData = new ArrayList<>();
            if (null != riskInfoDataModel.getTabnormalEnterprises() && riskInfoDataModel.getTabnormalEnterprises().size() > 0) {
                count += riskInfoDataModel.getTabnormalEnterprises().size();
                for (RiskInfoDataModel.RiskModel model : riskInfoDataModel.getTabnormalEnterprises()) {
                    model.setType(1);
                }
                totalData.addAll(riskInfoDataModel.getTabnormalEnterprises());
            }
            if (null != riskInfoDataModel.getThugeTaxPunishments() && riskInfoDataModel.getThugeTaxPunishments().size() > 0) {
                count += riskInfoDataModel.getThugeTaxPunishments().size();
                totalData.addAll(riskInfoDataModel.getThugeTaxPunishments());
            }
            data.get(3).setTotalData(totalData);
            tabData.get(3).setCount(count);
            setTabAdapter();
        });

        binding.jyycRecyclerView.setOnScrollChangeListener((view, i, i1, i2, i3) -> {
            int position = manager.findFirstVisibleItemPosition();
            if (position != mPos) {
                mPos = position;
                LinearLayoutManager linearLayoutManager = (LinearLayoutManager) binding.recyclerView.getLayoutManager();
                if (null != linearLayoutManager) {
                    linearLayoutManager.scrollToPositionWithOffset(position, 0);
                    for (CompanyLabelModel labelModel : tabData) {
                        labelModel.setSelect(false);
                    }
                    tabData.get(position).setSelect(true);
                    tabAdapter.notifyDataSetChanged();
                }
            }
        });
    }

    private void setTabAdapter() {
        tabAdapter = new InfoTabAdapter(context, tabData);
        binding.recyclerView.setAdapter(tabAdapter);
        tabAdapter.setOnTabItemClickListener(position -> {
            for (CompanyLabelModel labelModel : tabData) {
                labelModel.setSelect(false);
            }
            tabData.get(position).setSelect(true);
            tabAdapter.notifyDataSetChanged();
            LinearLayoutManager linearLayoutManager = (LinearLayoutManager) binding.jyycRecyclerView.getLayoutManager();
            if (null != linearLayoutManager) {
                linearLayoutManager.scrollToPositionWithOffset(position, 0);
            }
        });
        RiskInfoAdapter riskInfoAdapter = new RiskInfoAdapter(context, data);
        binding.jyycRecyclerView.setAdapter(riskInfoAdapter);
    }
}